package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.zjitc.entity.Permission;
import net.zjitc.entity.Role;
import net.zjitc.entity.User;
import net.zjitc.mapper.PermissionMapper;
import net.zjitc.mapper.RoleMapper;
import net.zjitc.mapper.UserMapper;
import net.zjitc.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther 郭梦婷
 * @create 2022-05-23 17:09:09
 * @describe 服务实现类
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public User findByUsername(String username) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", username);
        User user = userMapper.selectOne(wrapper);
        if (user == null) {
            return null;
        }
//        List<GrantedAuthority> list = new ArrayList<>();
//        //根据用户id查询对应角色
//        Role role=roleMapper.selectById(user.getRoleid());
//        if (role.getPsids()!=null){
//            String[] psids = role.getPsids().split(",");
//            for(String psid:psids){
//                Permission permission = permissionMapper.selectById(Integer.parseInt(psid));
//                role.getPermissions().add(permission);
//            }
//        }
//        user.setRoles(role);

        return user;
    }
}
